@*
This Razor view creates an XML sitemap used for search engines. Following these guidelines http://www.sitemaps.org
Paramaters
    MaxLevelForSitemap (int, optional, default 4)
*@

@using System.Text
@using System.Xml
@using umbraco.MacroEngines
@inherits DynamicNodeContext
@{
    // Get the root node
    var root = @Model.AncestorOrSelf();
    
    // Get the parameters passed by the macro. If not set, then default to 4
    var maxLevel = string.IsNullOrEmpty(Parameter.MaxLevelForSitemap) ? 4 : int.Parse(Parameter.MaxLevelForSitemap);

    // Let's save the content to a MemoryStream. You could use StringWriter too but you will have to deal with UTF-16 encoding
    using (var ms = new MemoryStream())
    {
        // Ensure the writer is UTF8 and nicely indented
        using (var writer = new XmlTextWriter(ms, Encoding.UTF8) { Formatting = Formatting.Indented })
        {
            // Write <?xml version="1.0" encoding="utf-8"?>
            writer.WriteStartDocument();
            
            //Write the xml
            writer.WriteStartElement("urlset");
            writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
            
            // Loop throught the child items 
            GenerateInternal(root, maxLevel, writer);
            
            // End writing the xml
            writer.WriteEndElement();
        }

        var xml = Encoding.UTF8.GetString(ms.ToArray());
        
        // change the mimetype for the current page to xml
        Context.Response.ContentType = "text/xml";
        Context.Response.Write(xml);
    }
}

@functions 
{
    public void GenerateInternal(dynamic parent, int maxLevel, XmlTextWriter writer)
    {
        WriteItem(parent, writer);
        if (parent.Level <= maxLevel)
        foreach (var child in parent.Children)
        {
            GenerateInternal(child, maxLevel, writer);
        }
    }

    public void WriteItem(dynamic item, XmlTextWriter writer)
    {
        // Filter any pages with a property named "umbracoNaviHide" that's set to 'true'
        if (!item.Visible)
        {
            return;
        }

        writer.WriteStartElement("url");
        
        // Get the full url including the host
        var url = new UriBuilder("http", Context.Request.ServerVariables["HTTP_HOST"]) {Path = item.Url};
        
        writer.WriteElementString("loc", url.ToString());
        writer.WriteElementString("lastmod", item.UpdateDate.ToUniversalTime().ToString("yyyy-MM-dd"));

        // you can extend the template and create changeFreq and priority fields if you need them in your sitemap
        if (item.HasValue("changeFreq"))
        {
            writer.WriteElementString("changefreq", item.ChangeFreq);
        }

        if (item.HasValue("priority"))
        {
            writer.WriteElementString("priority", item.Priority);
        }
        
        writer.WriteEndElement();
    }

}
